﻿using System;
using System.Collections.Generic;
using System.Text;
using Google.GData.Client;
using Google.GData.Contacts;

namespace ContactsTester
{
    internal sealed class GoogleContactConntector
    {
        public GoogleContactConntector()
        {
            
        }
        public event Sync.Events.SyncEvents.ConnectContacts OnConnectContacts;
        public GoogleContactConntector(string UserName,string Password)
        {
            OnConnectContacts += new Sync.Events.SyncEvents.ConnectContacts(Sync.Events.EventHandlers.GoogleContactConntector_OnConnectContacts);
            OnConnectContacts(new Sync.Events.ConnectEventArgs(UpdateType.Gmail));
            this._userName = UserName;
            this._password = Password;
            CurrentServiceConnection = GetGoogleContactService();
        }

        private ContactsService _currentServiceConnection;
        private readonly string _userName;
        private readonly string _password;
        private ContactsFeed _contactsFromGoogle;

        public ContactsService CurrentServiceConnection
        {
            get { return _currentServiceConnection; }
            set { _currentServiceConnection = value; }
        }

        public ContactsFeed ContactsFromGoogle
        {
            get { return _contactsFromGoogle; }
            set { _contactsFromGoogle = value; }
        }

        public SyncContact[] LoadGoogleContactDetails()
        {
            ContactsFromGoogle = new ContactsFeed(this._userName, new Service("ContactTestApp"));
            ContactsQuery cq = new ContactsQuery(ContactsQuery.CreateContactsUri(_userName, ContactsQuery.fullProjection));
            cq.NumberToRetrieve = 999;

            ContactsFromGoogle = CurrentServiceConnection.Query(cq);

            SyncContact[] currentItemsToSync = new SyncContact[ContactsFromGoogle.Entries.Count];
            int i = 0;
            foreach (ContactEntry o in ContactsFromGoogle.Entries)
            {
                SyncContact currentItem = new SyncContact();
                currentItem.FullName = o.Title.Text;
                currentItem.OriginalGoogleItem = o;
                foreach (Google.GData.Extensions.EMail email in o.Emails)
                {
                    if (email.Home)
                    {
                        currentItem.HomeNumber = email.Address;
                        continue;
                    }
                    if (email.Other)
                    {
                    }
                    if (email.Primary)
                    {
                        currentItem.EmailAddress = email.Address;
                        continue;
                    }
                    if (email.Work)
                    {
                        currentItem.WorkNumber = email.Address;
                        continue;
                    }
                }
                currentItemsToSync[i] = currentItem;
                i++;
            }

            return currentItemsToSync;
        }

        private ContactsService GetGoogleContactService()
        {
            ContactsService csservice = new ContactsService("ContactsTestApp");
            csservice.setUserCredentials(this._userName, this._password);
            return csservice;
        }
    }
}
